package com.lanxiao.cloud.modules.gateway.route.application.assembler;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lanxiao.cloud.common.core.dto.ResultModel;
import com.lanxiao.cloud.user.api.fill.BaseDtoFillHandleFactory;
import com.lanxiao.cloud.gateway.api.dto.GatewayRouteDubboDto;
import com.lanxiao.cloud.modules.gateway.route.application.dto.GatewayRouteCmdAdd;
import com.lanxiao.cloud.modules.gateway.route.application.dto.GatewayRouteCmdUpdate;
import com.lanxiao.cloud.modules.gateway.route.application.dto.GatewayRoutePageDto;
import com.lanxiao.cloud.modules.gateway.route.domain.model.GatewayRoute;
import java.util.List;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2021-09-09
 */
@Component
public class GatewayRouteAssembler {

  private final BaseDtoFillHandleFactory<Long> fillHandleFactory;

  public GatewayRouteAssembler(
      BaseDtoFillHandleFactory<Long> fillHandleFactory) {
    this.fillHandleFactory = fillHandleFactory;
  }

  public ResultModel<GatewayRoutePageDto> toGatewayRoutePageDto(IPage<GatewayRoute> page) {
    final List<GatewayRoutePageDto> gatewayRoutePageDtos = GatewayRouteConverter.INSTANCE.toGatewayRoutePageDto(
        page.getRecords());
    fillHandleFactory.fillHandle(gatewayRoutePageDtos);
    return new ResultModel<>(gatewayRoutePageDtos, (int) page.getTotal());
  }

  public List<GatewayRouteDubboDto> toGatewayRouteDubboDto(List<GatewayRoute> list) {
    return GatewayRouteConverter.INSTANCE.toGatewayRouteDubboDto(list);
  }

  public GatewayRoute toGatewayRoute(GatewayRouteCmdAdd gatewayRouteCmdAdd) {
    return GatewayRouteConverter.INSTANCE.toGatewayRoute(gatewayRouteCmdAdd);
  }

  public GatewayRoute toGatewayRoute(GatewayRouteCmdUpdate gatewayRouteCmdUpdate) {
    return GatewayRouteConverter.INSTANCE.toGatewayRoute(gatewayRouteCmdUpdate);
  }

  @Mapper
  public interface GatewayRouteConverter {
    GatewayRouteConverter INSTANCE = Mappers.getMapper(GatewayRouteConverter.class);

    @Mappings({
        @Mapping(source = "createBy", target = "createUser.id"),
        @Mapping(source = "updateBy",target = "updateUser.id")
    })
    GatewayRoutePageDto toGatewayRoutePageDto(GatewayRoute gatewayRoute);
    List<GatewayRoutePageDto> toGatewayRoutePageDto(List<GatewayRoute> gatewayRoutes);

    GatewayRouteDubboDto toGatewayRouteDubboDto(GatewayRoute gatewayRoute);
    List<GatewayRouteDubboDto> toGatewayRouteDubboDto(List<GatewayRoute> gatewayRoutes);

    GatewayRoute toGatewayRoute(GatewayRouteCmdAdd gatewayRouteCmdAdd);
    GatewayRoute toGatewayRoute(GatewayRouteCmdUpdate gatewayRouteCmdUpdate);

  }

}
